/* This file is part of xREL
 *
 * Copyright (C) 2012 Timo Zimmermann <xrel@timozimmermann.de>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

import QtQuick 1.1
import com.nokia.meego 1.0

import "delegates"
import "components"

Page {
    id: root

    property string name: "LatestP2PPage"
    property int per_page: 25
    property alias page_number: navigationBar.page
    property string category_id: ""
    property string group_id: ""
    property string ext_info_id: ""

    tools: mainToolBar

    XmlListModel {
        id: xmlCategoriesModel
        source: "http://api.xrel.to/api/p2p/categories.xml"
        query: "/p2p_categories/category"

        XmlRole { name: "name"; query: "meta_cat/string()" }
        XmlRole { name: "sub_cat"; query: "sub_cat/string()" }
        XmlRole { name: "id"; query: "id/string()" }
    }

    XmlListModel {
        id: xmlDisplayModel
        source: "http://api.xrel.to/api/p2p/releases.xml?per_page=" + root.per_page + "&page=" + root.page_number // + category_id !== "" ? "&category_id=" + category_id : ""
        query: "/p2p_releases/list/p2p_rls"

        XmlRole { name: "id"; query: "id/string()" }
        XmlRole { name: "dirname"; query: "dirname/string()" }
        XmlRole { name: "link_href"; query: "link_href/string()" }
        XmlRole { name: "category_meta_cat"; query: "category/meta_cat/string()" }
        XmlRole { name: "category_sub_cat"; query: "category/sub_cat/string()" }
        XmlRole { name: "category_id"; query: "category/id/string()" }
        XmlRole { name: "main_lang"; query: "main_lang/string()" }
        XmlRole { name: "pub_time"; query: "pub_time/string()" }
        XmlRole { name: "size_mb"; query: "size_mb/string()" }
        XmlRole { name: "group_name"; query: "group/name/string()" }
        XmlRole { name: "group_id"; query: "group/id/string()" }
        XmlRole { name: "ext_info_type"; query: "ext_info/type/string()" }
        XmlRole { name: "ext_info_id"; query: "ext_info/id/string()" }
        XmlRole { name: "ext_info_title"; query: "ext_info/title/string()" }
        XmlRole { name: "ext_info_link_href"; query: "ext_info/link_href/string()" }
        XmlRole { name: "ext_info_rating"; query: "ext_info/rating/string()" }
        XmlRole { name: "tv_season"; query: "tv_season/string()" }
        XmlRole { name: "tv_episode"; query: "tv_episode/string()" }
    }

    PageBackground { }

    PageHeader {
        id: pageHeader
        text: qsTr("P2P releases")
    }

    ListView {
        id: listView
        anchors.top: pageHeader.bottom
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.bottom: navigationBar.top
        cacheBuffer: per_page*80

        spacing: 6
        clip: true

        delegate: ReleaseP2PDelegate { }
        model: xmlDisplayModel

        onFlickEnded: navigationBar.show = atYEnd || atYBeginning

        XmlViewBusyIndicator { }

        function showReleaseP2PInfo(id) {
            pageStack.push(Qt.resolvedUrl("ReleaseP2PInfoPage.qml"), { id: id })
        }
    }

    NavigationBar {
        id: navigationBar
        onReload: xmlDisplayModel.reload()
    }
}
